
package com.jf.cloud.order.listener;

import com.jf.cloud.api.feign.SearchOrderFeignClient;
import com.jf.cloud.common.constant.SendTypeEnum;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.order.vo.SendNotifyBO;
import com.jf.cloud.common.product.constant.SupplierDeliveryType;
import com.jf.cloud.common.response.ResponseEnum;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.rocketmq.config.RocketMqConstant;
import com.jf.cloud.order.service.OrderService;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 确认收货
 * @author zz
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.ORDER_RECEIPT_TOPIC,consumerGroup = RocketMqConstant.ORDER_RECEIPT_TOPIC)
public class OrderReceiptConsumer implements RocketMQListener<Long> {
    private static final Logger logger = LoggerFactory.getLogger(OrderReceiptConsumer.class);

    @Autowired
    private RocketMQTemplate sendNotifyToShopTemplate;
    @Autowired
    private SearchOrderFeignClient searchOrderFeignClient;
    @Autowired
    private RocketMQTemplate sendNotifyToUserExtensionTemplate;

    /**
     * 订单确认收货监听
     */
    @Override
    public void onMessage(Long orderId) {

        logger.info("订单确认收货监听-ORDER_RECEIPT_TOPIC, order:{}",orderId);

        // 推送确认收货的通知
        ServerResponseEntity<List<SendNotifyBO>> orderServerResponseEntity = searchOrderFeignClient.listSendNotifyByOrderIds(Collections.singletonList(orderId));
        if (orderServerResponseEntity.isFail()) {
            throw new LuckException(orderServerResponseEntity.getMsg());
        }
        SendNotifyBO sendNotifyBO = orderServerResponseEntity.getData().get(0);
        sendNotifyBO.setSendType(SendTypeEnum.RECEIPT_ORDER.getValue());
        SendStatus sendStockStatus = sendNotifyToShopTemplate.syncSend(RocketMqConstant.SEND_NOTIFY_TO_SHOP_TOPIC, new GenericMessage<>(sendNotifyBO)).getSendStatus();
        if (!Objects.equals(sendStockStatus,SendStatus.SEND_OK)) {
            throw new LuckException(ResponseEnum.EXCEPTION);
        }
        //供应商发货的订单  同时发送确认收货提醒给商家和供应商
        if(Objects.equals(sendNotifyBO.getSupplierDeliveryType(), SupplierDeliveryType.SUPPLIER_DELIVERY.value())){
            sendNotifyBO.setShopId(null);
            sendNotifyBO.setSendType(SendTypeEnum.RECEIPT_ORDER_TO_SUPPLIER.getValue());
            SendStatus sendToSupplierStatus = sendNotifyToShopTemplate.syncSend(RocketMqConstant.SEND_NOTIFY_TO_SHOP_TOPIC, new GenericMessage<>(sendNotifyBO)).getSendStatus();
            if (!Objects.equals(sendToSupplierStatus,SendStatus.SEND_OK)) {
                throw new LuckException(ResponseEnum.EXCEPTION);
            }
        }



        // 推送用户积分、成长值增加的通知
        SendStatus growthSendStatus = sendNotifyToUserExtensionTemplate.syncSend(RocketMqConstant.SEND_NOTIFY_TO_USER_EXTENSION_TOPIC, new GenericMessage<>(orderId)).getSendStatus();
        if (!Objects.equals(growthSendStatus,SendStatus.SEND_OK)) {
            throw new LuckException(ResponseEnum.EXCEPTION);
        }
    }
}
